//
//  TCSGFWStory.m
//  StoryGrabber
//
//  Created by Thomas on 8/20/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TCSGFWStory.h"
#import "TCSGGeneralMethods.h"
#define DEBUG	YES
@interface TCSGFWStory (private)
- (NSString *)getHTMLWithCookies:(NSString *)url;
- (NSString *)getMultipleStory:(NSString *)storyID;

- (NSString *)getSingleChapterStory:(NSString *)page;

- (NSString *)getAuthor:(NSString *)content;
- (NSString *)getTitle:(NSString *)content;
- (NSString *)getSummary :(NSString *)content;
- (NSString *)getGenre :(NSString *)content;
- (NSArray *)getChapters:(NSString *)content;
- (NSString *)getSingleChapter:(NSString *)content;
@end

@implementation TCSGFWStory
-(int)version
{
	return 0.1;
}
-(NSString *)hostName
{
	return @"Ficwad.com";
}
-(NSString *)shortHostName
{
	return @"FW";
}
-(BOOL)respondsToAuthor
{
	return NO;
}
-(NSImage *)hostImage
{
	return [[NSImage alloc] initWithContentsOfFile:[NSBundle pathForResource:@"FW" ofType:@"png" inDirectory:[[NSBundle bundleForClass:[self class]] bundlePath] ]];
}
-(NSString *)pluginAuthor
{
	return @"Thomas Cool";
}
-(NSString *)base
{
	return @"http://www.ficwad.com/story/";
}
-(NSArray *)getStoriesArrayForAuthor:(NSString *)authorID
{
	return [NSArray array];
}
-(NSDictionary *)getStory:(NSString *)storyID
{
	NSString *content = [self getHTMLWithCookies:[NSString stringWithFormat:@"%@%@",[self base],storyID,nil]];
	//NSLog(@"content: %@",content);
	NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
	//Through Subsequent Ranging find the author Name;
	
	[returnDict setObject:[self getAuthor:content] forKey:AUTHORKEY];
	
	//Now the story Title
	[returnDict setObject:[self getTitle:content] forKey:TITLEKEY];
	[returnDict setObject:[self getSummary:content] forKey:SUMMARYKEY];
	[returnDict setObject:[self getGenre:content] forKey:GENREKEY];
	[returnDict setObject:[self hostName] forKey:HOSTKEY];
	[returnDict setObject:[NSString stringWithFormat:@"%@_%@",[self shortHostName],storyID,nil] forKey:HTMLCODEKEY];
	[returnDict setObject:storyID forKey:IDKEY];
	NSLog(@"returnDict: %@",returnDict);
	NSRange multipart = [content rangeOfString:@"<form enctype=\"multipart/form-data\""];
	if(multipart.location==NSNotFound)
	{
		NSString *story = [self getSingleChapterStory:content];
		[returnDict setObject:story forKey:STORYKEY];
		[returnDict setObject:[NSArray arrayWithObject:story] forKey:ARRAYKEY];
		[story release];
	}
	else
	{
		NSLog(@"multiple");
		NSString *tempString = [[[[content componentsSeparatedByString:@"/story/"] objectAtIndex:1] componentsSeparatedByString:@"\">"] objectAtIndex:0];
		NSString *index = [self getHTMLWithCookies:[NSString stringWithFormat:@"%@%@",[self base],tempString,nil]];
		[returnDict setObject:[self getSummary:index] forKey:SUMMARYKEY];
		[returnDict setObject:[self getTitle:index] forKey:TITLEKEY];
		NSLog(@"returnDict: %@",returnDict);
		[index release];
		[tempString release];
		NSLog(@"getting Chapters");
		NSArray *chapters = [self getChapters:content];
		NSLog(@"afterChapters: %@",chapters);
		NSMutableArray *tempStoryArray = [[NSMutableArray alloc] init];
		int i;
		NSMutableString *string = [[NSMutableString alloc] initWithString:@""];
		for (i=0;i<[chapters count];i++)
		{
			//[[self getHTMLWithCookies:[self getHTMLWithCookies:[NSString stringWithFormat:@"%@%@.html",[self base],[chapters objectAtIndex:i],nil]]] writeToFile:@"/Users/tom/Desktop/kkk.html" atomically:YES];
//[self getHTMLWithCookies:[NSString stringWithFormat:@"%@%@.html",[self base],[chapters objectAtIndex:i],nil]]
			NSString *tempString = [self getSingleChapter:[self getHTMLWithCookies:[NSString stringWithFormat:@"%@%@",[self base],[chapters objectAtIndex:i],nil]]];
			[string appendString:[NSString stringWithFormat:@"<h2 class=\"Chapter\">Part %i</h2>%@\n\n",i+1,tempString,nil]];
			[tempStoryArray addObject:tempString];
			[tempString release];
		}
		string = [NSMutableString stringWithFormat:@"<html>\n<head>\n<title>%@</title>\n</head>\n<body>\n%@\n</body></html>",[returnDict objectForKey:TITLEKEY],string,nil];
		[returnDict setObject:string forKey:STORYKEY];
		[returnDict setObject:tempStoryArray forKey:ARRAYKEY];
		[string release];
		[tempStoryArray release];
		
		
	}
	//NSLog(@"returnDict: %@",returnDict);

	//
	return returnDict;
}
-(NSArray *)getChapters:(NSString *)content
{
	NSString *tempString = [[[[content componentsSeparatedByString:@"<select name=\"goto\">"] objectAtIndex:1] componentsSeparatedByString:@"</select>"] objectAtIndex:0];
	NSMutableArray *tempArray = [[tempString componentsSeparatedByString:@"<option value=\"/story/"] mutableCopy];
	[tempString release];
	int i;
	for(i=0;i<[tempArray count];i++)
	{
		[tempArray replaceObjectAtIndex:i withObject:[[[tempArray objectAtIndex:i] componentsSeparatedByString:@"\""] objectAtIndex:0]];
	}
	[tempArray removeObjectAtIndex:0];
	[tempArray removeObjectAtIndex:0];
	return [tempArray autorelease];
	
	
}
-(NSString *)getHTMLWithCookies:(NSString *)url
{
	NSString *content;
	if(!DEBUG)
	{
		NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
		//NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
		NSURLResponse *response;
		NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
		content = [[NSString alloc]  initWithBytes:[data bytes]
													  length:[data length] encoding: NSUTF8StringEncoding];
	}
	else
	{
		content = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSASCIIStringEncoding error:nil];
	}
	
	return [content autorelease];
}
-(NSString *)getAuthor:(NSString *)content
{
	NSRange tempRange = [content rangeOfString:@"<span class=\"author\">"];
	tempRange.location = tempRange.location+tempRange.length;
	tempRange.length = [content length] - tempRange.location;
	NSString *tempString = [content substringWithRange:tempRange];
	tempRange = [tempString rangeOfString:@">"];
	NSRange tempRangeEnd = [tempString rangeOfString:@"</a></span>"];
	tempRange.location = tempRange.location+tempRange.length;
	tempRange.length = tempRangeEnd.location-tempRange.location;
	NSString *author = [tempString substringWithRange:tempRange];
	return author;
}
-(NSString *)getTitle:(NSString *)content
{
	NSString *tempString = [[content componentsSeparatedByString:@" ::"] objectAtIndex:0];
	return [[tempString componentsSeparatedByString:@"<title>"] objectAtIndex:1];
}
-(NSString *)getSummary :(NSString *)content
{
	NSString *tempString= [[content componentsSeparatedByString:@"<blockquote class=\"summary\"><p>"] objectAtIndex:1];
	return [[tempString componentsSeparatedByString:@"</p>"]objectAtIndex:0];
	}
-(NSString *)getGenre :(NSString *)content
{
	NSString *tempString= [[content componentsSeparatedByString:@"Genres:&nbsp;"] objectAtIndex:1];
	return [[tempString componentsSeparatedByString:@" -"]objectAtIndex:0];
}
-(NSString *)getSingleChapterStory:(NSString *)content
{
	NSString *tempString = [[content componentsSeparatedByString:@"<div id=\"storytext\">"] objectAtIndex:1];
	
	return [[tempString componentsSeparatedByString:@"<div id=\"storyfoot\">"]objectAtIndex:0];
}
-(NSString *)getSingleChapter:(NSString *)content
{
	NSString *tempString = [[content componentsSeparatedByString:@"<div id=\"storytext\">"] objectAtIndex:1];
	NSLog(@"single Chapter");
	return [NSString stringWithFormat:@"<p>\n%@\n</p>",[[[[[tempString componentsSeparatedByString:@"<div class=\"chapterform\">"]objectAtIndex:0] componentsSeparatedByString:@"</div>"] objectAtIndex:0] stringByReplacingOccurrencesOfString:@"<br />\n" withString:@"</p>\n<p>"]];
}
@end
